package com.teame.readbaby.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.teame.readbaby.vo.Address;
import com.teame.readbaby.vo.Invoice;
import com.teame.readbaby.vo.OrderDetail;
import com.teame.readbaby.vo.Produt;

public class OrderDetailParser extends BaseParser<OrderDetail> {

	private static final String TAG = "OrderDetailParser";

	@Override
	public OrderDetail parseJSON(String paramString) throws JSONException {
		if (paramString != null) {
			String result = checkResponse(paramString);
			if (result == null) {
				// TODO 弹出吐司或对话框提示用户
				return null;
			}

			String str = "";
			JSONObject jsonObject = JSONObject.parseObject(paramString);

			OrderDetail orderDetail = new OrderDetail();

			str = jsonObject.getString("address_info");
			Address address_info = JSONObject.parseObject(str, Address.class);
			orderDetail.setAddress(address_info);

			str = jsonObject.getString("checkout_prom");
			List<Object> checkout_prom = JSONObject.parseArray(str);
			orderDetail.setCheckout_prom(checkout_prom);

			str = jsonObject.getString("invoice_info");
			Invoice invoice_info = JSONObject.parseObject(str, Invoice.class);
			orderDetail.setInvoice(invoice_info);

			str = jsonObject.getString("payment_info");
			JSONObject objPayment_info = JSONObject.parseObject(str);
			int type = objPayment_info.getInteger("type");
			orderDetail.setPayment_info(type);

			str = jsonObject.getString("checkout_addup");
			JSONObject objCheckout_addup = JSONObject.parseObject(str);
			type = objCheckout_addup.getInteger("freight");
			orderDetail.setFreight(type);
			type = objCheckout_addup.getInteger("prom_cut");
			orderDetail.setProm_cut(type);
			type = objCheckout_addup.getInteger("total_count");
			orderDetail.setTotal_count(type);
			type = objCheckout_addup.getInteger("total_point");
			orderDetail.setTotal_point(type);
			type = objCheckout_addup.getInteger("total_price");
			orderDetail.setTotal_price(type);

			// ====================================================================

			// str = jsonObject.getString("productlist");
			// List<Produt> produtlist = JSONObject.parseArray(str,
			// Produt.class);

			str = jsonObject.getString("productlist");
			JSONArray parseArray = JSONObject.parseArray(str);
			List<Produt> produtlist = new ArrayList<Produt>();
			
			Produt produt = null;
			for (int i = 0; i < parseArray.size(); i++) {
				produt = new Produt();
				String string = parseArray.getString(i);
				JSONObject parseObject = JSONObject.parseObject(string);
				String id = parseObject.getString("id");
				String isgift = parseObject.getString("isgift");
				String name = parseObject.getString("name");
				int number = parseObject.getInteger("number");
				String pic = parseObject.getString("pic");
				float price = parseObject.getFloat("price");
				int uplimit = parseObject.getInteger("uplimit");

				string = parseObject.getString("product_property");
				JSONArray product_property = JSONObject.parseArray(string);
				for (int j = 0; j < product_property.size(); j++) {
					String string2 = product_property.getString(j);
					JSONObject parseObject2 = JSONObject.parseObject(string2);
					String key = parseObject2.getString("key");
					if ("颜色".equals(key)) {
						String color = parseObject2.getString("value");
						produt.setColor(color);
					} 
					if ("大小".equals(key)) {
						String size = parseObject2.getString("value");
						produt.setSize(size);
					}
				}
				
				produt.setId(id);
				produt.setIsgift(isgift);
				produt.setName(name);
				produt.setNumber(number);
				produt.setPic(pic);
				produt.setPrice(price);
				produt.setUplimit(uplimit);

				produtlist.add(produt);
			}

			orderDetail.setProductlist(produtlist);

			return orderDetail;
		}
		return null;
	}

}
